{% extends "base.html" %}
{% set active_page = "operations" %}
{% block content %}
<form action="{{ url_for('operations') }}" method="post">
  {{ form.hidden_tag() }}
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title"></h3>
    </div>
    <div class="panel-body">
      <table>
        <tr>
          <td>{{ form.hostname(size=25, class="form-control", placeholder="hostname") }}</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>{{ form.cmd(size=50, class="form-control", placeholder="command") }}</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td><button type="submit" class="btn btn-primary" name="do_action" value="run" onclick="$('#loading').show();$('#hiding').hide();">Run</button></td>
        </tr>
      </table>
    </div>
  </div>
</form>

<div id="loading" style="display:none;">
  <input type="image" src="{{ url_for('static',filename='img/loading.gif') }}">
</div>

<div id="hiding">
  {% if failed_host %}
    <div class="alert alert-danger" role="alert">
      <b>ERROR:</b> Failed to connect to host <b>{{ failed_host }}</b> port 22: Network is unreachable.
    </div>
  {% elif failed_auth %}
    <div class="alert alert-danger" role="alert">
      <b>ERROR:</b> Failed to connect to host <b>{{ hostname }}</b> port 22: Authentication failed.
    </div>
  {% elif blacklisted_word %}
    <div class="alert alert-danger" role="alert">
      <b>ERROR:</b> The blacklisted word/character <b>{{ blacklisted_word }}</b> in command.
    </div>
  {% elif failed_cmd %}
    <div class="alert alert-danger" role="alert">
      <b>ERROR:</b> Failed to execute command <b>'{{ cmd }}'</b> on host <b>{{ hostname }}</b>.
    </div>
    <div class="panel panel-danger">
      <div class="panel-heading">
        <h3 class="panel-title">Output</h3>
      </div>
      <div class="panel-body">
        {% if out.stderr %}
          <pre class="text-danger bg-danger">{{ out.stderr }}</pre>
        {% endif %}
        {% if out.stdout %}
          <pre class="text-danger bg-danger">{{ out.stdout }}</pre>
        {% endif %}
      </div>
    </div>
  {% elif succeeded_cmd %}
    <div class="alert alert-success" role="alert">
      <b>OK:</b> Succeeded to execute command <b>'{{ cmd }}'</b> on host <b>{{ hostname }}</b>.
    </div>
    <div class="panel panel-success">
      <div class="panel-heading">
        <h3 class="panel-title">Output</h3>
      </div>
      <div class="panel-body">
        <pre class="text-success bg-success">{{ out.stdout }}</pre>
      </div>
    </div>
  {% endif %}
</div>
{% endblock %}
